"重置按键
" nmap <silent> <Leader>b <Leader>b
" nmap <silent> <Leader>b :BufExplorer<CR>




imap jk <Esc>
nnoremap tn  :tabnew<CR>
nnoremap tj  :tabnext<CR>
nnoremap tk  :tabprev<CR>
nnoremap tc  :tabclose<CR>
nnoremap J 5j
nnoremap K 5k

nnoremap <leader>h :bp<cr>
nnoremap <leader>l :bn<cr>

map S :w<CR>
map Q :q<CR>
map <F4> :AddHeader<CR>

map <Leader><CR> :noh<CR>

map <leader>si :set splitright<CR>:vsplit<CR>
map <leader>sn :set nosplitright<CR>:vsplit<CR>
map <leader>su :set nosplitbelow<CR>:split<CR>
map <leader>se :set splitbelow<CR>:split<CR>

map <c-up> :res +5<CR>
map <c-down> :res -5<CR>
map <c-left> :vertical resize-5<CR>
map <c-right> :vertical resize+5<CR>
"map <Leader>b :TCommentBlock<CR>
" Rope AutoImport and OrganizeImport
" nnoremap <C-S-o> :RopeOrganizeImports<CR>0<CR><CR>
" nnoremap <C-S-i> :RopeAutoImport<CR>

map <silent> <leader>e :NERDTreeToggle<CR>
map <silent> <C-w> :NERDTreeFind<CR>
nmap <leader>nt :NERDTreeFind<CR>

inoremap <silent><expr> <TAB>
            \ coc#pum#visible() ? coc#pum#next(1):
            \ CheckBackspace() ? "\<Tab>" :
            \ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"

" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice.
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
            \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

" Use <c-space> to trigger completion.
" inoremap <silent><expr> <c-space> coc#refresh()
inoremap <silent><expr> <c-\> coc#refresh()


" Use `[c` and `]c` to navigate diagnostics
nmap <silent> [c <Plug>(coc-diagnostic-prev)
nmap <silent> ]c <Plug>(coc-diagnostic-next)

" Remap keys for gotos
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)

" Use K to show documentation in preview window
nnoremap <silent> <leader>k :call <SID>show_documentation()<CR>
function! s:show_documentation()
    if (index(['vim','help'], &filetype) >= 0)
        execute 'h '.expand('<cword>')
    else
        call CocAction('doHover')
    endif
endfunction

let g:UltiSnipsExpandTrigger="<c-l>"
let g:UltiSnipsListSnippets="<c-tab>"
let g:UltiSnipsJumpForwardTrigger="<c-j>"
let g:UltiSnipsJumpBackwardTrigger="<c-k>"

nnoremap <Leader>p :call Fzf_dev()<cr>
nnoremap <Leader>. :Ag<cr>

" nnoremap <Leader><Leader>a :CocCommand prettier.forceFormatDocument<cr>
" autocmd FileType java nnoremap <Leader><Leader>a :Autoformat<cr>
nnoremap <Leader><Leader>a :Autoformat<cr>
" nnoremap <Leader><Leader>a :silent %!prettier --stdin-filepath --tab-width=4 %<cr>

let g:neoformat_enabled_groovy = ['npmgroovylint']
let g:neoformat_groovy_npmgroovylint = {
  \ 'exe': 'npm-groovy-lint',
  \ 'args': ['--format', '--files', '%:p', '--no-insight', '--verbose'],
  \ 'replace': 1,
  \ 'stdin': 0
  \ }

" Split remapping
nnoremap <C-j> <C-W><C-J>
nnoremap <C-k> <C-W><C-K>
nnoremap <C-l> <C-W><C-L>
nnoremap <C-h> <C-W><C-H>

nnoremap gcm :w<CR>:G commit -a <CR>
nnoremap gp :G push <CR>

nmap <leader>rn <Plug>(coc-rename)

nnoremap <silent> <C-p>  :<C-u>CocList -A --normal yank<cr>

" nnoremap <silent> <c-j> <c-y>
" nnoremap <silent> <c-k> <c-e>
vnoremap uy "+y
" noremap <leader>fb :<C-U><C-R>=printf("Leaderf buffer %s", "")<CR><CR>
" noremap <leader>fm :<C-U><C-R>=printf("Leaderf mru %s", "")<CR><CR>
" noremap <leader>ft :<C-U><C-R>=printf("LeaderfTag %s", "")<CR><CR>
" noremap <leader>fl :<C-U><C-R>=printf("Leaderf line %s", "")<CR><CR>
" noremap <leader>fr :<C-U><C-R>=printf("Leaderf rg %s", "")<CR><CR>


" noremap <leader>ff :<C-U><C-R>=printf("Leaderf function %s", "")<CR><CR>
vnoremap Y "+y

nnoremap <leader>w :w<cr>
" nnoremap <leader>t :b#<cr>
nnoremap <silent>[b :b#<cr>
nnoremap <silent>[s <Plug>(easymotion-s)
nnoremap <silent>[w <Plug>(easymotion-w)
nnoremap <silent>[W <Plug>(easymotion-W)
nnoremap <space>q :qa<cr>
" nnoremap <space>b :<C-U><C-R>=printf("Leaderf buffer %s", "")<CR><CR>
nnoremap <space>f :<C-U><C-R>=printf("LeaderfFunction %s", "")<CR><CR>
nnoremap <space>q :qa<cr>

nnoremap <leader>g :call BufExplorer()<CR>




au FileType python noremap <F6> :!python3 %<cr>
au FileType python noremap <silent> <F7> :w!<cr>:silent .w !tmux neww /bin/bash -c "python3 %; read i"<cr>
au FileType python noremap <silent> <F2> :!ctags -R *.py<CR>
au FileType python imap <silent> <F7> <Esc>:w!<cr>:silent .w !tmux neww /bin/bash -c "python3 %; read i"<cr>a
au FileType python imap <silent> <F7> <Esc>:w!<cr>:silent .w !tmux neww /bin/bash -c "python3 %; read i"<cr>a
au FileType groovy noremap <silent> <F7> :w!<cr>:silent .w !tmux neww /bin/bash -c "groovy %; read i"<cr>
au FileType go noremap <silent> <F7> :w!<cr>:silent .w !tmux neww /bin/bash -c "go build -o main && ./main; read i"<cr>
au FileType go imap <silent> <F7> <Esc>:w!<cr>:silent .w !tmux neww /bin/bash -c "go build -o main && ./main; read i"<cr>a
au FileType java noremap <buffer> <silent> <F7> <Esc>:wa!<cr>:silent .w !tmux neww /bin/bash -c "mvn spring-boot:run; read i"<cr>
" au FileType json noremap <silent> <Leader><Leader>a :%! python3 -m json.tool<CR>

autocmd FileType json noremap <buffer> <silent> <Leader><Leader>a :%! python3 -m json.tool<CR>


" Applying code actions to the selected code block
" Example: `<leader>aap` for current paragraph
xmap <leader>a  <Plug>(coc-codeaction-selected)
nmap <leader>a  <Plug>(coc-codeaction-selected)

" Remap keys for applying code actions at the cursor position
nmap <leader>ac  <Plug>(coc-codeaction-cursor)
" Remap keys for apply code actions affect whole buffer
nmap <leader>as  <Plug>(coc-codeaction-source)
" Apply the most preferred quickfix action to fix diagnostic on the current line
nmap <leader>qf  <Plug>(coc-fix-current)

" Remap keys for applying refactor code actions
nmap <silent> <leader>re <Plug>(coc-codeaction-refactor)
xmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)
nmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)

" Run the Code Lens action on the current line
nmap <leader>cl  <Plug>(coc-codelens-action)

" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)

if has('nvim-0.4.0') || has('patch-8.2.0750')
    " nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
    " nnoremap <silent><nowait><expr> <C-g> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
    nnoremap <silent><nowait><expr> <C-f> coc#float#has_float() ? coc#float#scroll(1) : "\<C-f>"
    nnoremap <silent><nowait><expr> <C-g> coc#float#has_float() ? coc#float#scroll(0) : "\<C-b>"
    inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
    inoremap <silent><nowait><expr> <C-g> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
    vnoremap <silent><nowait><expr> <C-f> coc#float#has_float() ? coc#float#scroll(1) : "\<C-f>"
    vnoremap <silent><nowait><expr> <C-g> coc#float#has_float() ? coc#float#scroll(0) : "\<C-b>"
endif

" Git
nmap ]h <Plug>(GitGutterNextHunk)
nmap [h <Plug>(GitGutterPrevHunk)
omap ih <Plug>(GitGutterTextObjectInnerPending)
omap ah <Plug>(GitGutterTextObjectOuterPending)
xmap ih <Plug>(GitGutterTextObjectInnerVisual)
xmap ah <Plug>(GitGutterTextObjectOuterVisual)

" nmap <buffer> <Leader>is <Plug>(GitGutterStageHunk)
" nmap <buffer> <Leader>iu <Plug>(GitGutterUndoHunk)
" nmap <buffer> <Leader>ip <Plug>(GitGutterPreviewHunk)

nnoremap <silent> <Leader>rts :call TrimWhiteSpace()<CR>
nmap tl :exe "tabn ".g:lasttab<CR>
cmap <c-f> <c-f>
" let g:coc_snippet_next = '<c-j>'


autocmd FileType mysql,sql vmap <Leader>o :'<,'>DB <cr>
autocmd FileType mysql,sql nnoremap <Leader>o <S-v>:'<,'>DB <cr>

vmap gb :'<,'>TCommentBlock<CR>
nmap <silent> <Leader>p :Files<CR>
" nmap <silent> <Leader>b :BufExplorer<CR>
nmap <silent> <Leader>f :Leaderf function --reverse<CR>
nmap <silent> <Leader>m :Mru<CR>
nmap <silent> <Leader>t :Leaderf tag --reverse<CR>
nmap <silent> <Leader>. :Rg<CR>
nnoremap <silent>[b :b#<CR>


nnoremap <silent> <C-s> :!/bin/bash -c 'sudo mocp --stop' &>/dev/null<CR><CR>
nnoremap <silent> <C-LEFT> :!/bin/bash -c 'sudo mocp -S;sudo mocp -r;sudo mocp -i \| grep STOP && sudo mocp -p' &>/dev/null<CR><CR>
nnoremap <silent> <C-RIGHT> :!/bin/bash -c 'sudo mocp -S;sudo mocp -f;sudo mocp -i \| grep STOP && sudo mocp -p' &>/dev/null<CR><CR>


" function! s:create_file_by_tmux()
"     execute '!tmux display-popup -E -w 80\%  '..'/bin/bash -c "echo > /tmp/tmp.file; echo please type the filename to create: ;read -r -e f; [ -n \"\$f\" ] &&  (touch \$f ; echo \$f > /tmp/tmp.file)"'
"     execute 'edit '..system("cat /tmp/tmp.file")
" endfunction


function! s:run_c()
    execute ":w"
    execute '!make && ./main.out'
endfunction

function! s:run_c_plus()
    " execute ":w"
    " execute 'silent !tmux popup -E -w 80\% -h 80\% /bin/bash -c "g++ -o main.out *.cpp && ./main.out ; read"'
    execute ":w"
    execute '!g++ -o main.out % && ./main.out'
endfunction


function! s:run_c_patten()
    execute ":w"
    let current_dir = getcwd()
    execute 'silent !tmux popup -E -w 80\% -h 80\% /bin/bash -c "cd '..current_dir..';gcc -o main.out *.c && ./main.out;echo type any key to continue; read -n 1"'
    " execute ":w"
    " execute '!g++ -o main.out *.cpp && ./main.out'
endfunction


function! s:run_c_plus_patten()
    execute ":w"
    let current_dir = getcwd()
    execute 'silent !tmux popup -E -w 80\% -h 80\% /bin/bash -c "cd '..current_dir..';g++ -o main.out *.cpp && ./main.out;echo type any key to continue; read -n 1"'
    " execute ":w"
    " execute '!g++ -o main.out *.cpp && ./main.out'
endfunction

function! s:play_music()
    " execute '!tmux popup -E -w 80\% -w 80\% /bin/bash -c "mocp"'
    execute '![ `runlevel|awk ''{print $NF}''` -eq 5 ] && tmux -2 popup -E -w 80\% -w 80\% /bin/bash -c "mocp" || tmux -2 popup -E -w 80\% -w 80\% /bin/bash -c "sudo mocp"'
    " execute 'silent !tmux popup -E -w 80\% -h 80\% /bin/bash -c "music_name=\$(ls /share_mount | fzf); echo \$music_name; mocp -l /share_mount/\"\${music_name}\";"'
endfunction
" nnoremap <silent> <C-e> :call <SID>create_file_by_tmux()<CR><CR>
au BufWinEnter *.cpp nnoremap <silent> <F9> :call <SID>run_c_plus()<CR>
au BufWinEnter *.py nnoremap <silent> <F9> :w<cr>:!python3 %<CR>
au BufWinEnter *.cpp nnoremap <silent> <F10> :call <SID>run_c_plus_patten()<CR>
au BufWinEnter *.c nnoremap <silent> <F8> :call <SID>run_c()<CR><CR>
au BufWinEnter *.c nnoremap <silent> <F10> :call <SID>run_c_patten()<CR>
nnoremap <silent> <F3> :call <SID>play_music()<CR><CR>


" 用于打开错误调试
nnoremap <silent><nowait> <space>d  :<C-u>CocList diagnostics<cr>



